package com.bear.balance.jdbc.imp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.bear.PlatFormDao.balance.MoneyTypeDao;
import com.bear.model.entity.balance.MoneyType;

@Repository
public class MoneyTypeDaoImp implements MoneyTypeDao{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<MoneyType> getAlltypesByuserid(int userid) {
		String sqlStr = "select id,typedesc" +
				" from moneytype where userid=?";
		final List<MoneyType> moneytypes = new ArrayList<MoneyType>();
		jdbcTemplate.query(sqlStr,new Object[]{userid},
				new RowCallbackHandler() {
					@Override
					public void processRow(ResultSet rs) throws SQLException {
							MoneyType moneytype = new MoneyType();
							moneytype.setId(rs.getInt("id"));
							moneytype.setTypeDesc(rs.getString("typedesc"));
							moneytypes.add(moneytype);
					}
				});
		return moneytypes;
	}

	@Override
	public void addType(int id, String typestr2) {
		String sqlStr = "insert into moneytype (typedesc,userid)" +
				" values(?,?) ";
		Object[] args= {typestr2,id};
		jdbcTemplate.update(sqlStr,args);
	}

	@Override
	public String getTypeNameByid(int id) {
		String query = "select typedesc from moneytype where id="+id;
		return jdbcTemplate.queryForObject(query, String.class);
	}

}
